package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.FollowUser;
import com.tanhua.dubbo.server.pojo.PageInfo;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.service.IdService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/22 14:29
 * @version: 0.0.1
 * @modified By:
 */
@Service(version = "1.0.0")
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    @Override
    public String saveVideo(Video video) {
        //校验
        if(null==video.getUserId()){
            return null;
        }
        try {
            video.setId(ObjectId.get());
            //自增长id
            video.setVid(this.idService.createId("video",video.getId().toHexString()));
            video.setCreated(System.currentTimeMillis());
            this.mongoTemplate.save(video);
            return video.getId().toHexString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Video> queryVideoList(Integer page, Integer pageSize) {
        PageRequest pageRequest=PageRequest.of(page-1,pageSize, Sort.by(Sort.Order.desc("created")));
        Query query=new Query().with(pageRequest);
        List<Video> videoList = this.mongoTemplate.find(query, Video.class);
        PageInfo<Video>pageInfo=new PageInfo<>();
        pageInfo.setTotal(0); //不提供总数
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(videoList);
        return pageInfo;
    }

    @Override
    public Boolean followUser(Long userId, Long followUserId) {
        try {
            FollowUser followUser=new FollowUser();
            followUser.setId(ObjectId.get());
            followUser.setUserId(userId);
            followUser.setFollowUserId(followUserId);
            followUser.setCreated(System.currentTimeMillis());
            this.mongoTemplate.save(followUser);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean disFollowUser(Long userId, Long followUserId) {
        Query query=Query.query(Criteria.where("userId").is(userId).and("followUserId").is(followUserId));
        DeleteResult deleteResult = this.mongoTemplate.remove(query, FollowUser.class);
        return deleteResult.getDeletedCount()>0;
    }

    @Override
    public Video queryVideoById(String videoId) {
        Video video = this.mongoTemplate.findById(new ObjectId(videoId), Video.class);
        return video;
    }

    @Override
    public List<Video> queryVideoListByVids(List<Long> vidList) {
        Query query=Query.query(Criteria.where("vid").in(vidList));
        return this.mongoTemplate.find(query,Video.class);
    }
}
